package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.services.IBoradService;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
public class BoradController {
    //从配置文件中读取值 默认是9
    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Resource
    private IBoradService iBoradService;
    //查询首页版本列表
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("首页版本个数："+indexBoardNum);
        //调用Service查询
        List<Board> boards = iBoradService.selectByNum(indexBoardNum);
        if (boards == null){
            //判断非空
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    @GetMapping("/getById")
    public AppResult<Board> getById (@NonNull Long id){
        //调用Service
        Board board = iBoradService.selectById(id);
        if (board == null || board.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_BANNED));
        }
        return AppResult.success();
    }
}
